var express = require('express');
var router = express.Router();
var http = require("http");
var request = require('request');

/* GET home page. */
router.get('/', function (req, res, next) {

  res.render('index', { title: 'Ex111press' });
});


router.get('/getkey', function (req, res, next) {
  let str = "";
  request('http://i.cque.edu.cn/cas/v2/getPubKey/', function (error, response, body) {
    str = response.headers['set-cookie']
    console.log(str)
    mod = JSON.parse(body)
    console.log(mod.modulus)
    console.log(mod.exponent)
    res.json([{ "cookie": str, "modulus": mod.modulus, "exponent": mod.exponent }])
  })
});

let username = ""
let password = ""
let cj = ""
let wj = ""
let address = ""
let dm = ""
let execution = ""
let pv = ""
let session = ""
let pf = ""
let pm = ""
let pc = ""
let IDP = ""
let ASPsession = ""
let location = ""
let ticket = ""


router.get('/login', function (req, res, next) {
  username = req.query.username
  password = req.query.password
  cj = req.query.cj
  wj = req.query.wj
  address = req.query.address
  dm = req.query.dm
  execution = req.query.execution
  pv = req.query.pv
  session = req.query.session

  // console.log(req.query)

  let options = {
    'method': 'POST',
    'url': 'http://i.cque.edu.cn/cas/login?service=http://xsc.cque.edu.cn:866/Mobile/login.aspx',
    'headers': {
      // 'Host': 'i.cque.edu.cn',
      // 'Origin': 'http://i.cque.edu.cn',
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',

      'Content-Type': 'application/x-www-form-urlencoded',
      'Cookie': 'JSESSIONID=' + session + ';_pv0=' + pv
    },
    form: {
      'username': username,
      'password': password,
      'authcode': '',
      'execution': execution,
      '_eventId': 'submit'
    }
  };

  request(options, function (error, response) {
    if (error) throw new Error(error);
    console.log(response.headers);
    let str = response.headers['set-cookie']
    pf = str[1].slice(5, -67)
    pm = str[3].slice(5, -28)
    IDP = str[4].slice(20, -28)
    location = response.headers['location']
    console.log(location)
    ticket = response.headers['location'].slice(52)
    if(location) {
      res.send(location)
    }
  })
});




router.get('/login2', function (req, res, next) {
  location = req.query.lo
  let options = {
    'method': 'GET',
    'url': 'http://xsc.cque.edu.cn:866/Mobile/login.aspx?ticket=ST-874834-InJP2wfDikb52PwSeZh9-zfsoft.com',
    // 'url': req.query.lo,
    'maxRedirects': 10,
    'headers': {
      // 'Host': 'xsc.cque.edu.cn:866',
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
      'Cookie': 'ASP.NET_SessionId=;_pv0=1Nf8T36VcC9mAh%2BOBzMw5onFSjvJEpF4Wu%2BGtvVFgW7jQ1c46IRjUyGCtyJHdaLrSd7b84Mx1SSqNVq1oyHDqkawmUUz25XNQrd07c%2FKMrzJOxLz02jSLGLZqFnDV3Z0OkNPNRrSDmXGfKemsGXHPnM%2F%2BBKKJTLF3LK3d3wv%2FUNrFURq%2FtPREUV8K%2BNLwyVSu895mGORlk%2Fn78ldC5MG1YrOm44zDIC8Kh1lsCKFLJN0CIMTs22eLtdV7ygxuoIkv9oyIlB0gPiDsYyo8ujKW7xGJv09hzfAGLtv4tApi7LFepjx6Zo8dHOOTc%2FUlqqIhc8JDN5Be0K5aQ5abptfHzVF1A32ULoXGREXhVfKyC2UNkUm%2BiETpIKo7vlSYHv9cjKLFWGfaXjBe26Dp%2BuDX4T4JVRTJhRtTyclca5HBUM%3D;_pf0=L91fy%2BlXt7mUw5hPiNKmlw%3D%3D;_pc0=oVjuvRQIoOpra6QRo9iw%2BLIC2IMIsa3MpA6gLbw0afU%3D;iPlanetDirectoryPro==90gKIDlkl8kAw8Ud6kcxt9niv5F1tKRaDBONydBv3d3o%2FHHwJ8f2TTQey5YsDzYErf688qx4NJaqciYdQmzYV3Qlo1%2BjupAez8YgkG4ybqBAmmBxgE3%2B0U4GRGDXp7%2F9WPpa46o7YoQ%2FxxjkRHwTg23C0oZF7JH5KrHpt20ofQ7UshNBMq98qrzTByC5BDLhhmxkJT2CK9bxn53LkMyJMHwS9%2Fl%2FBy8SVPX4RXwoPVD5VEb5Nxf1NrKdVOlCZXyvmETyrRpPEAtG7ojRSqIfJWZvY5hHPujM0ygwsc9BW9rarinveZoLEitXxvxRvXd2XlnwiO4AKRP6%2FMw87AJCQrK44ctM%2FWLqxjwDFKdeo4w%3D'
      // 'Cookie': 'ASP.NET_SessionId=;_pv0=' + pv + ';_pf0=' + pf + ';_pc0=' + pc + ';iPlanetDirectoryPro=' + IDP
    }
  };


  request(options, function (error, response) {
    // console.log(location)
    if (error) throw new Error(error);
    console.log(response.body);
    console.log(response.headers);
    res.send("11")
  })


});

module.exports = router;
